package cn.iocoder.yudao.module.reading.convert.profile;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reading.controller.app.profile.vo.*;
import cn.iocoder.yudao.module.reading.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.reading.framework.util.StaticResourceUrlUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 个人资料 Convert
 *
 * @author 李晨
 */
@Mapper
public interface ProfileConvert {

    ProfileConvert INSTANCE = Mappers.getMapper(ProfileConvert.class);

    /**
     * 转换为用户基本信息响应 VO
     *
     * @param user 用户DO
     * @param staticResourceUrlUtils URL转换工具
     * @return 用户基本信息响应 VO
     */
    default ProfileInfoRespVO convertToProfileInfo(UserDO user, StaticResourceUrlUtils staticResourceUrlUtils) {
        ProfileInfoRespVO result = BeanUtils.toBean(user, ProfileInfoRespVO.class);
        if (result.getAvatar() != null && staticResourceUrlUtils != null) {
            result.setAvatar(staticResourceUrlUtils.toFullUrl(result.getAvatar()));
        }
        return result;
    }

    /**
     * 转换为用户详细信息响应 VO
     *
     * @param user 用户DO
     * @param staticResourceUrlUtils URL转换工具
     * @return 用户详细信息响应 VO
     */
    default ProfileDetailRespVO convertToProfileDetail(UserDO user, StaticResourceUrlUtils staticResourceUrlUtils) {
        ProfileDetailRespVO result = BeanUtils.toBean(user, ProfileDetailRespVO.class);
        if (result.getAvatar() != null && staticResourceUrlUtils != null) {
            result.setAvatar(staticResourceUrlUtils.toFullUrl(result.getAvatar()));
        }
        return result;
    }

    /**
     * 转换为用户更新DO
     *
     * @param reqVO 更新请求VO
     * @return 用户DO
     */
    default UserDO convertToUserDO(ProfileUpdateReqVO reqVO) {
        UserDO userDO = BeanUtils.toBean(reqVO, UserDO.class);
        // 不直接复制avatar字段，在service层处理URL转换
        userDO.setAvatar(null);
        return userDO;
    }

}
